{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from neo4j import Transaction, Session, GraphDatabase\n",
    "\n",
    "from graph_copy import graph_copy\n",
    "from neo4jexplorer import Neo4jExplorer"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "HIST_URI = \"neo4j+s://99c1a702.databases.neo4j.io:7687\"\n",
    "LOCAL_URI = \"neo4j://127.0.0.1:7687\"\n",
    "USER = \"neo4j\"\n",
    "PSWD = \"231099\"\n",
    "hist_driver = GraphDatabase.driver(HIST_URI, auth=(USER, PSWD))\n",
    "local_driver = GraphDatabase.driver(LOCAL_URI, auth=(USER, \"23109900\"))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "      n_name      n_din\n",
      "0  6.68-30-1  6.68-30-1\n",
      "1  6.68-31-1  6.68-31-1\n",
      "2  6.68-30-2  6.68-30-2\n",
      "3  6.68-31-2  6.68-31-2\n",
      "4  6.68-30-3  6.68-30-3\n",
      "       n_din      m_din  weight\n",
      "0  6.68-30-1  6.68-31-1       1\n",
      "1  6.68-31-1  6.68-13-1       1\n",
      "2  6.68-31-1  6.68-33-1       1\n",
      "3  6.68-30-2  6.68-31-2       1\n",
      "4  6.68-31-2  6.68-33-1       1\n"
     ]
    }
   ],
   "source": [
    "from one_GESN_graph_creation import main\n",
    "\n",
    "# main()\n",
    "graph_copy(hist_driver.session(), local_driver.session())"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "133\n",
      "4x loops deleted\n"
     ]
    },
    {
     "data": {
      "text/plain": "118"
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "app = Neo4jExplorer()#uri=LOCAL_URI)\n",
    "print(len(app.get_all_dins()))\n",
    "app.del_loops()\n",
    "hist_gesns = app.get_all_dins()\n",
    "len(hist_gesns)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "(720, 4)"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.read_excel(\"./../new_data/Сметы Берзарина.xls\")\n",
    "df1.rename(\n",
    "    columns={\"Проект\": \"wbs1\", \"Смета\": \"wbs2\", \"Шифр\": \"wbs3_id\", \"Наименование\": \"name\"},\n",
    "    inplace=True\n",
    ")\n",
    "df1 = df1[[\"wbs1\", \"wbs2\", \"wbs3_id\", \"name\"]]\n",
    "df1.dropna(subset=[\"wbs3_id\"], inplace=True)\n",
    "df1.drop_duplicates(inplace=True)\n",
    "df1.shape"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "56"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_gesns = df1.wbs3_id.unique()\n",
    "targ_gesns = np.intersect1d(hist_gesns, input_gesns)\n",
    "len(targ_gesns)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"3.47-3-15\" in targ_gesns"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4x loops deleted\n",
      "deleted: 15.1-2201-03\n",
      "deleted: 15.1-2201-04\n",
      "deleted: 15.1-2300-02\n",
      "deleted: 15.1-2500-01\n",
      "deleted: 15.2-41-7\n",
      "deleted: 3.47-34-8\n",
      "deleted: 6.68-30-5\n",
      "deleted: 6.68-31-5\n",
      "deleted: 15.1-2102-02\n",
      "deleted: 3.47-9-10\n",
      "deleted: 3.47-11-1\n",
      "deleted: 3.47-9-5\n",
      "deleted: 3.8-1-1\n",
      "deleted: 3.22-49-1\n",
      "deleted: 3.4-6-10\n",
      "deleted: 3.29-1587-2\n",
      "deleted: 3.29-1588-1\n",
      "deleted: 3.29-1590-2\n",
      "deleted: 3.29-1591-1\n",
      "deleted: 3.4-10-2\n",
      "deleted: 6.69-8-1\n",
      "deleted: 3.16-9-2\n",
      "deleted: 3.16-9-3\n",
      "deleted: 3.23-13-1\n",
      "deleted: 6.69-8-2\n",
      "deleted: 3.16-15-2\n",
      "deleted: 3.7-45-1\n",
      "deleted: 3.24-2-1\n",
      "deleted: 3.24-2-3\n",
      "deleted: 3.8-2-6\n",
      "deleted: 3.27-54-1\n",
      "deleted: 3.7-46-3\n",
      "deleted: 3.22-71-1\n",
      "deleted: 3.24-36-2\n",
      "deleted: 3.24-38-1\n",
      "deleted: 3.24-40-1\n",
      "deleted: 3.24-40-2\n",
      "deleted: 3.24-41-1\n",
      "deleted: 3.24-41-2\n",
      "deleted: 3.24-42-1\n",
      "deleted: 3.24-42-2\n",
      "deleted: 3.24-43-1\n",
      "deleted: 3.4-19-36\n",
      "deleted: 3.24-43-2\n",
      "deleted: 3.24-44-1\n",
      "deleted: 3.24-44-2\n",
      "deleted: 3.4-8-53\n",
      "deleted: 3.23-1-5\n",
      "deleted: 3.26-27-1\n",
      "deleted: 4.11-1-4\n",
      "deleted: 4.11-3-1\n",
      "deleted: 4.8-170-3\n",
      "deleted: 3.23-24-1\n",
      "deleted: 3.23-13-3\n",
      "deleted: 4.8-75-3\n",
      "deleted: 3.16-15-1\n",
      "deleted: 3.16-9-1\n",
      "deleted: 3.7-21-2\n",
      "deleted: 3.6-8-1\n",
      "deleted: 3.22-70-3\n",
      "deleted: 3.22-72-1\n",
      "deleted: 3.22-72-2\n"
     ]
    }
   ],
   "source": [
    "app.create_new_graph_algo(targ_gesns)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "56"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(app.get_all_dins())"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "<neo4j._sync.work.result.Result at 0x1d3d1d488b0>"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "app.driver.session().run(\n",
    "\"\"\"\n",
    "match (n)-[r]->(m)\n",
    "where n.DIN = '3.47-7-8' AND m.DIN = '3.47-3-15'\n",
    "delete r\n",
    "\"\"\"\n",
    ")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
